/*
 * FaceCat图形通讯框架(非开源)
 * 著作权编号:2015SR229355+2020SR0266727
 * 上海卷卷猫信息技术有限公司
 */

#include "stdafx.h"
#include "DateTitle.h"

namespace FaceCat{
    DateTitle::DateTitle(FCCalendar *calendar){
        m_calendar = calendar;
        setBackColor(FCColor_None);
        setBorderColor(FCColor_None);
        FCFont font(L"Default", 22, true, false, false);
        setFont(&font);
        FCSize size = {180, 30};
        setSize(size);
    }
    
    DateTitle::~DateTitle(){
        m_calendar = 0;
    }
    
    FCCalendar* DateTitle::getCalendar(){
        return m_calendar;
    }
    
    void DateTitle::setCalendar(FCCalendar *value){
        m_calendar = value;
    }
    
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    
    String DateTitle::getViewType(){
        return L"DateTitle";
    }
    
    void DateTitle::onClick(FCTouchInfo touchInfo){
        FCButton::onClick(touchInfo);
        if (m_calendar){
            FCCalendarMode mode = m_calendar->getMode();
            if (mode == FCCalendarMode_Day){
                m_calendar->setMode(FCCalendarMode_Month);
            }
            else if (mode == FCCalendarMode_Month){
                m_calendar->setMode(FCCalendarMode_Year);
            }
            m_calendar->update();
            m_calendar->invalidate();
        }
    }
    
    void DateTitle::onPaintForeground(FCPaint *paint, const FCRect& clipRect){
        if(m_calendar){
            int width = getWidth(), height = getHeight();
            FCFont *font = getFont();
            String text;
            FCCalendarMode mode = m_calendar->getMode();
            wchar_t yMText[100] = {0};
            if (mode == FCCalendarMode_Day){
                CMonth *month = m_calendar->getMonth();
                text = FCTran::intToStr(month->getYear()) + L"年" + FCTran::intToStr(month->getMonth()) + L"月";
            }
            else if(mode == FCCalendarMode_Month){
                text = FCTran::intToStr(m_calendar->getMonthDiv()->getYear()) + L"年";
            }
            else if(mode == FCCalendarMode_Year){
                int startYear = m_calendar->getYearDiv()->getStartYear();
                text = FCTran::intToStr(startYear) + L"年 - " + FCTran::intToStr(startYear + 11) + L"年";
            }
            FCSize tSize = paint->textSize(text, font, -1);
            FCRect tRect = {0};
            tRect.left = (width - tSize.cx) / 2;
            tRect.top = (height - tSize.cy) / 2;
            tRect.right = tRect.left + tSize.cx + 1;
            tRect.bottom = tRect.top + tSize.cy + 1;
            paint->drawText(text, getPaintingTextColor(), font, tRect, -1);
        }
    }
}
